<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\common\AdminController;

class Upload extends AdminController
{
    public function index()
    {
        $pagination = $this->_get_pagination_data();

        $pagination['rows_count'] = model('admin/upload')
            ->where($pagination['where_map'])->count();

        $pagination['total_pages'] = ceil($pagination['rows_count'] / $pagination['page_size']);
        $pagination['page_num'] = $pagination['page_num'] > $pagination['total_pages'] ? 1 : $pagination['page_num'];

        $upload_list = model('admin/upload')
            ->where($pagination['where_map'])
            ->page($pagination['page_num'], $pagination['page_size'])
            ->order($pagination['sort_field'], $pagination['sort_order'])
            ->select();

        $this->assign('upload_list', $upload_list);
        $this->assign('pagination', $pagination);
        $this->display('upload/upload_list');
    }






    /**
     * 上传
     */
    public function upload()
    {
        $return = json_encode(model('admin/Upload')->upload());
        exit($return);
    }

    /**
     * 下载
     */
    public function download($token)
    {
        if (empty($token)) {
            $this->error('token参数错误！');
        }

        //解密下载token
        $file_md5 = \lyf\Crypt::decrypt($token, user_md5(is_login()));
        if (!$file_md5) {
            $this->error('下载链接已过期，请刷新页面！');
        }

        $upload_object = model('Upload');
        $file_id       = $upload_object->getFieldByMd5($file_md5, 'id');
        if (!$upload_object->download($file_id)) {
            $this->error($upload_object->getError());
        }
    }

    public function delete()
    {
        exit(json_encode([]));
    }

    /**
     * KindEditor编辑器文件管理
     */
    public function fileManager()
    {
        exit(model('Upload')->fileManager());
    }
}
